package com.gxa.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.domain.entity.PromotionProduct;
import com.gxa.domain.vo.PromotionProductVO;
import com.gxa.mapper.PromotionProductMapper;
import com.gxa.service.PromotionProductService;
import org.springframework.stereotype.Service;
import java.util.List;
import java.math.BigDecimal;
import java.util.Objects;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import com.gxa.domain.entity.Product;
import com.gxa.domain.entity.PromotionType;
import com.gxa.mapper.ProductMapper;
import com.gxa.service.PromotionTypeService;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class PromotionProductServiceImpl extends ServiceImpl<PromotionProductMapper, PromotionProduct> implements PromotionProductService {
    
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private PromotionTypeService promotionTypeService;

    @Override
    public List<PromotionProductVO> getPromotionProduct() {
        List<PromotionProductVO> list = baseMapper.selectPromotionProduct();
        // @调试日志，打印每个VO的promotionPrice
        for (PromotionProductVO vo : list) {
            System.out.println("promotionPrice: " + vo.getPromotionPrice());
        }
        return list;
    }

    @Override
    public BigDecimal getBestPromotionPrice(String productSerialNo, String barcode) {
        QueryWrapper<PromotionProduct> wrapper = new QueryWrapper<>();
        if (productSerialNo != null && !productSerialNo.isEmpty()) {
            wrapper.eq("product_serial_no", productSerialNo);
        }
        if (barcode != null && !barcode.isEmpty()) {
            wrapper.eq("barcode", barcode);
        }
        List<PromotionProduct> activities = this.list(wrapper);
        return activities.stream()
                .map(PromotionProduct::getPromotionPrice)
                .filter(Objects::nonNull)
                .min(BigDecimal::compareTo)
                .orElse(null);
    }

    @Override
    public List<PromotionProductVO> getPromotionProductByBarcode(String barcode) {
        QueryWrapper<PromotionProduct> wrapper = new QueryWrapper<>();
        wrapper.eq("barcode", barcode);
        List<PromotionProduct> activities = this.list(wrapper);
        List<PromotionProductVO> voList = new ArrayList<>();
        for (PromotionProduct pp : activities) {
            PromotionProductVO vo = new PromotionProductVO();
            BeanUtils.copyProperties(pp, vo);
            // 补充商品名
            try {
                Integer serialNo = Integer.parseInt(pp.getProductSerialNo());
                Product product = productMapper.findProductById(serialNo);
                if (product != null) {
                    vo.setProductName(product.getProductName());
                } else {
                    vo.setProductName("未知商品");
                }
            } catch (Exception e) {
                vo.setProductName("未知商品");
            }
            // 补充活动类型名
            PromotionType type = promotionTypeService.getById(pp.getPromotionTypeId());
            if (type != null) {
                vo.setPromotionTypeName(type.getName());
            } else {
                vo.setPromotionTypeName("未知类型");
            }
            voList.add(vo);
        }
        return voList;
    }
} 